package org.dromara.quick.chat.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.dromara.common.core.domain.R;
import org.dromara.common.model.enums.channel.ChannelTypeEnum;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.wukong.service.ChannelService;
import org.dromara.common.wukong.service.WuKongConversationService;
import org.dromara.quick.chat.model.bo.ImChatConversationReadMaxSeqBO;
import org.dromara.quick.chat.service.ImChatConversationService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 最近会话表实体类
 * @Author:         Stars
 * @Date:           2025/5/18 21:09
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/im/chat/conversation")
public class ImChatConversationController {

    private final ImChatConversationService imChatConversationService;
    private final ChannelService channelService;
    private final WuKongConversationService wuKongConversationService;

    /**
     * 更改阅读最大消息数量
     */
    @PostMapping("/update/read/max/seq")
    public R<?> updateReadMaxSeq(@RequestBody ImChatConversationReadMaxSeqBO imChatConversationReadMaxSeqBO) {
        if (imChatConversationReadMaxSeqBO.getMessageSeq() == null) {
            return R.fail("消息序列号不能为空");
        }
        Long userId = LoginHelper.getUserId();
        imChatConversationReadMaxSeqBO.setSenderId(userId);
        imChatConversationService.updateReadMaxSeq(imChatConversationReadMaxSeqBO);
        return R.ok();
    }

    /**
     * 更改删除最大消息数量
     * @param imChatConversationReadMaxSeqBO
     * @return
     */
    @PostMapping("/update/delete/max/seq")
    public R<?> updateDeleteMaxSeq(@RequestBody ImChatConversationReadMaxSeqBO imChatConversationReadMaxSeqBO) throws IOException {
        Long userId = LoginHelper.getUserId();
        imChatConversationReadMaxSeqBO.setSenderId(userId);


        // 获取当前频道的最大消息id，因为自己主动发送的是没有消息id的。
        if (ChannelTypeEnum.PERSONAL_CHANNEL.getCode() == imChatConversationReadMaxSeqBO.getChannelType()){
            // 个人频道
            JSONArray jsonArray = channelService.syncOfflineMessage(userId.toString(), imChatConversationReadMaxSeqBO.getReceiverId().toString(), -1L, 1L);
            if (CollectionUtils.isNotEmpty(jsonArray)){
                JSONObject conversation = (JSONObject) jsonArray.get(0);
                Integer messageSeq = conversation.getInteger("message_seq");
                imChatConversationReadMaxSeqBO.setDeleteMessageSeq(Long.valueOf(messageSeq)+1);
            }
        }else {
            // 群组频道
            JSONArray jsonArray = channelService.syncOfflineGroupRecentlyMessage(userId.toString(), imChatConversationReadMaxSeqBO.getReceiverId().toString(), 0L,1L);
            if (CollectionUtils.isNotEmpty(jsonArray)){
                JSONObject conversation = (JSONObject) jsonArray.get(0);
                Integer messageSeq = conversation.getInteger("message_seq");
                imChatConversationReadMaxSeqBO.setDeleteMessageSeq(Long.valueOf(messageSeq)+1);
            }
        }
        if (imChatConversationReadMaxSeqBO.getDeleteMessageSeq() == null) {
            return R.fail("删除消息失败");
        }
        // 删除会话列表
        wuKongConversationService.deleteConversation(userId, imChatConversationReadMaxSeqBO.getReceiverId().toString(), imChatConversationReadMaxSeqBO.getChannelType());
        imChatConversationService.updateDeleteMaxSeq(imChatConversationReadMaxSeqBO);
        return R.ok();
    }
}
